<!--
 * @Descripttion: 弹窗扩展组件
 * @version: 2.0
 * @Author: sakuya
 * @Date: 2021年8月27日08:51:52
 * @LastEditors: sakuya
 * @LastEditTime: 2022年5月14日15:13:41
-->

<template>
  <div class="sc-dialog" ref="scDialog">
    <!-- :style="{ width: diaLogSize }" -->
    <el-dialog
      ref="dialog"
      :style="{ width: isFullscreen ? '100%' : diaLogSize }"
      v-model="dialogVisible"
      :fullscreen="isFullscreen"
      v-bind="$attrs"
      :show-close="false"
    >
      <template #header>
        <slot name="header">
          <span class="el-dialog__title">{{ title }}</span>
        </slot>
        <div class="sc-dialog__headerbtn">
          <button
            v-if="showFullscreen"
            aria-label="fullscreen"
            type="button"
            @click="setFullscreen"
          >
            <el-icon v-if="isFullscreen" class="el-dialog__close"
              ><el-icon-bottom-left
            /></el-icon>
            <el-icon v-else class="el-dialog__close"
              ><el-icon-full-screen
            /></el-icon>
          </button>
          <button
            v-if="showClose"
            aria-label="close"
            type="button"
            @click="closeDialog"
          >
            <el-icon class="el-dialog__close"><el-icon-close /></el-icon>
          </button>
        </div>
      </template>
      <div v-loading="loading">
        <ElScrollbar :style="dialogStyle">
          <slot></slot>
        </ElScrollbar>
      </div>
      <template #footer>
        <slot name="footer"></slot>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { unref } from "vue";
import { isNumber } from "@/utils/is";
import { ElScrollbar } from "element-plus";
export default {
  props: {
    size: { type: Number, default: 2 },
    modelValue: { type: Boolean, default: false },
    title: { type: String, default: "" },
    showClose: { type: Boolean, default: true },
    showFullscreen: { type: Boolean, default: true },
    maxHeight: { type: String, default: "400px" },
    loading: { type: Boolean, default: false },
  },
  components: {
    ElScrollbar,
  },
  data() {
    return {
      dialogHeight: isNumber(this.maxHeight)
        ? `${this.maxHeight}px`
        : this.maxHeight,
      dialogVisible: false,
      isFullscreen: false,
    };
  },

  computed: {
    diaLogSize() {
      return this.size == 1 ? "30%" : this.size === 2 ? "50%" : "70%";
    },
    dialogStyle() {
      return {
        height: unref(this.dialogHeight),
      };
    },
  },
  watch: {
    modelValue() {
      this.dialogVisible = this.modelValue;
      if (this.dialogVisible) {
        this.isFullscreen = false;
      }
    },
    isFullscreen(val) {
      if (val) {
        const windowHeight = document.documentElement.offsetHeight;
        this.dialogHeight = `${
          windowHeight - 55 - 60 - (this.$slots.footer ? 63 : 0)
        }px`;
      } else {
        isNumber(this.maxHeight) ? `${this.maxHeight}px` : this.maxHeight;

        if (this.size === 1) {
          this.dialogHeight = isNumber(this.maxHeight)
            ? `${this.maxHeight}px`
            : this.maxHeight;
        }
      }
    },
  },
  mounted() {
    this.dialogVisible = this.modelValue;
  },
  methods: {
    //关闭
    closeDialog() {
      this.dialogVisible = false;
    },
    //最大化
    setFullscreen() {
      this.isFullscreen = !this.isFullscreen;
    },
  },
};
</script>

<style scoped>
.sc-dialog__headerbtn {
  position: absolute;
  top: 24px;
  /* top: var(--el-dialog-padding-primary); */
  right: var(--el-dialog-padding-primary);
}
.sc-dialog__headerbtn button {
  padding: 0;
  background: transparent;
  border: none;
  outline: none;
  cursor: pointer;
  font-size: var(--el-message-close-size, 16px);
  margin-left: 15px;
  color: var(--el-color-info);
}
.sc-dialog__headerbtn button:hover .el-dialog__close {
  color: var(--el-color-primary);
}
.sc-dialog:deep(.el-dialog).is-fullscreen {
  display: flex;
  flex-direction: column;
  top: 0px !important;
  left: 0px !important;
}
.sc-dialog:deep(.el-dialog) .el-dialog__header {
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  margin-right: 0;
}
.sc-dialog:deep(.el-dialog).is-fullscreen .el-dialog__header {
  border-bottom: 1px solid #f0f0f0;
  margin-right: 0;
}
.sc-dialog:deep(.el-dialog).is-fullscreen .el-dialog__body {
  flex: 1;
  overflow: auto;
}
.sc-dialog:deep(.el-dialog) .el-dialog__footer {
  padding-bottom: 10px;
  border-top: 1px solid #f0f0f0;
}
.sc-dialog:deep(.el-dialog).is-fullscreen .el-dialog__footer {
  padding-bottom: 10px;
  border-top: 1px solid var(--el-border-color-base);
}
</style>
